iT邦幫忙

第 11 屆 iThome 鐵人賽

DAY 4
0
自我挑戰組

網路世界的奇怪冒險系列 第 4

【Day 4】 在Visual Studio 2017 寫一個簡單的班表(1/3)

  • 分享至 

  • xImage
  •  

大家好,今天跟大家一起學習如何在Visual Studio 2017 寫一個簡單的班表。
預計會分成三部分講解,大家敬請期待。

Step1 .建立專案

https://ithelp.ithome.com.tw/upload/images/20190920/20112000OVnDUahTx5.png

https://ithelp.ithome.com.tw/upload/images/20190920/20112000LEw53Woski.png

Step2.建立LocalDB
請參考:
在Visual Studio 2017 建立 localDB資料庫 (1/2)
在Visual Studio 2017 建立 localDB資料庫 (2/2)

Ste3. Using 使用的套件

using System.Configuration;
using System.Data;
using System.Data.SqlClient;

Step4. 寫撈出本週的方法

        /********************/
        /*     Function*     /
        /********************/
        public string[] Get_Week_List()  //撈本週的日期
        {
            string[] re_arr = { "", "", "", "", "", "", "", "" };
            SqlCommand cmd = new SqlCommand();
            SqlConnection conn = new SqlConnection(strConnString);
            cmd.Connection = conn;

            DataTable dt = new DataTable();
      

            string sqlStr = @"
                 select 
                        convert(varchar,DATEADD(week, DATEDIFF(week, '', getDate()), ''), 111)  as 'w1',
                        convert(varchar,DATEADD(day, +1, DATEADD(week, DATEDIFF(week, '', getDate()), '')),111) as 'w2',
                        convert(varchar,DATEADD(day, +2, DATEADD(week, DATEDIFF(week, '', getDate()), '')),111) as 'w3',
                        convert(varchar,DATEADD(day, +3, DATEADD(week, DATEDIFF(week, '', getDate()), '')),111) as 'w4',
                        convert(varchar,DATEADD(day, +4, DATEADD(week, DATEDIFF(week, '', getDate()), '')),111) as 'w5',
                        convert(varchar,DATEADD(day, +5, DATEADD(week, DATEDIFF(week, '', getDate()), '')),111) as 'w6',
                        convert(varchar,DATEADD(day, -1, DATEADD(week, DATEDIFF(week, '', getDate()), '')),111) as 'w7',
                        '0'  as 'diffweek'
        
   ";

            conn.Open();
            cmd.CommandText = sqlStr;

            SqlDataReader sread = cmd.ExecuteReader();
            if (sread.HasRows)
            {
                while (sread.Read())
                {
                    re_arr[0] = sread["w1"].ToString();
                    re_arr[1] = sread["w2"].ToString();
                    re_arr[2] = sread["w3"].ToString();
                    re_arr[3] = sread["w4"].ToString();
                    re_arr[4] = sread["w5"].ToString();
                    re_arr[5] = sread["w6"].ToString();
                    re_arr[6] = sread["w7"].ToString();
                    re_arr[7] = sread["diffweek"].ToString();

                }
            }
            cmd.Dispose();
            conn.Dispose();

            return re_arr;

        }

Step5. 寫一個顯示頁面

   public ActionResult ShowWeek()
        {
            string[] week = Get_Week_List();

            ViewBag.w1 = week[0];
            ViewBag.w2 = week[1];
            ViewBag.w3 = week[2];
            ViewBag.w4 = week[3];
            ViewBag.w5 = week[4];
            ViewBag.w6 = week[5];
            ViewBag.w7 = week[6];

            return View();
        }

https://ithelp.ithome.com.tw/upload/images/20190920/20112000cDMThUPPfC.png

Step 6. 顯示畫面(http://localhost:1501/Home/ShowWeek) 1501:為隨機的數字 請自行帶入數字

View的程式碼:


@{
    ViewBag.Title = "ShowWeek";
}

<h2>ShowWeek</h2>

星期一:  @ViewBag.w1<br />
星期二:@ViewBag.w2<br />
星期三:@ViewBag.w3<br />
星期四:@ViewBag.w4<br />
星期五:@ViewBag.w5<br />
星期六:@ViewBag.w6<br />
星期日:@ViewBag.w7<br />

https://ithelp.ithome.com.tw/upload/images/20190920/20112000b1crDAOBM7.png

https://ithelp.ithome.com.tw/upload/images/20190920/20112000L003wNodam.png


上一篇
【Day 3】 練習撈出一週日期的SQL語法
下一篇
【Day 5】 在Visual Studio 2017 寫一個簡單的班表(2/3)
系列文
網路世界的奇怪冒險30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言